package org.hackystat.devcathlon.ui.page.scoreboard.halloffame;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.ui.DevcathlonSession;

/**
 * Display the developer with most points events to the Hall of Fame.
 * 
 * @author John Ancheta, Scheller Sanchez
 */
public class MostTeamPointsPanel extends Panel {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * This Panel displays the developer with the most points for Hall of Fame.
   * 
   * @param id The Wicket id from the html markup.
   */
  public MostTeamPointsPanel(String id) {
    super(id);
    
    final HashMap<String, Integer> mapMostTeamPoints = new HashMap<String, Integer>();
    Collection<Match> matches = MatchManager.getInstance().getAllMatches();
    
    for (Match match : matches) {
      Set<Team> teams = match.getTeams();
      Team winner = null;
      int highScore = 0;
      for (Team team : teams) {
        int totalScore = match.getTotalScore(team);
        if (winner == null) {
          winner = team;
          highScore = totalScore;
        }
        else if (highScore < totalScore) {
          winner = team;
          highScore = totalScore;
        }
        else if (highScore == totalScore) {
          winner = null;
        }
        mapMostTeamPoints.put(team.getName() + "(" + match.getName() + ")", 
            Integer.valueOf(totalScore));
      }
    }
    
    final List<String> listMostTeamPoints = new ArrayList<String>();
    Set<Map.Entry<String, Integer>> mapentryMapMostTeamPoints = mapMostTeamPoints.entrySet();
    
    int rank = 0; // The rank of the event.
    
    while (!mapentryMapMostTeamPoints.isEmpty()) {
      Map.Entry<String, Integer> most = null;
      for (Map.Entry<String,Integer> mapentry : mapentryMapMostTeamPoints) {
        if (most == null) {
          most = mapentry;
        }
        if (most.getValue().intValue() < mapentry.getValue().intValue()) {
          most = mapentry;
        }
      }
      rank++;
      listMostTeamPoints.add(rank + ".  " + most.getKey() + " (" + 
          most.getValue().toString() + " points)");
      if (DevcathlonSession.get().getEmail() == null) {
        mapentryMapMostTeamPoints.clear();
      }
      else {
        mapentryMapMostTeamPoints.remove(most);
      }
    }
    
    DataView dataview = new DataView("ItemList", new ListDataProvider(listMostTeamPoints)) {
      private static final long serialVersionUID = 1L;
      
      @Override
      protected void populateItem(Item item) {
        final String topTeams = (String) item.getModelObject();
        item.add(new Label("labelMostTeamPoints", topTeams));
      }
    };
    add(dataview);    
  }
}
